﻿using BuildExplorer.Properties;
using Microsoft.TeamFoundation.Build.Client;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;

namespace BuildExplorer
{
    static class Util
    {
        public static string Truncate(this string value, int maxLen)
        {
            if (string.IsNullOrEmpty(value) == true) { return value; }
            if (value.Length > maxLen) { return value.Substring(0, maxLen); }
            return value;
        }

        public static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute),
                false);

            return attributes.Any() ? attributes[0].Description : value.ToString();
        }

        public static string FormatValue(string value)
        {
            // First, try parsing as timespan. Otherwise, a timespan would get parsed as a date and get displayed incorrectly
            TimeSpan span;
            if (TimeSpan.TryParse(value, CultureInfo.InvariantCulture, out span))
            {
                return value;
            }

            DateTime date;
            if (DateTime.TryParse(value,
                CultureInfo.InvariantCulture,
                DateTimeStyles.AdjustToUniversal,
                out date))
            {
                if (date.Kind == DateTimeKind.Utc)
                {
                    // StartTime/FinishTime are stored in format "yyyy-mm-ddThh:mm:ss.xxxxZ", which correctly converts to UTC.
                    return FormatDateTime(date);
                }
                else
                {
                    // Other properties that were originally DateTime are stored in "mm/dd/yyyy h:m AM/PM", which converts to "unspecified".
                    // In this case, we don't know the real datetime, so don't convert.
                    return value;
                }
            }

            return value;
        }

        public static string FormatDateTime(DateTime date, TimeSpan? duration = null)
        {
            Debug.Assert(date.Kind == DateTimeKind.Utc);

            if (date.Date == DateTime.MinValue.Date)  // Can't compare entire DateTime because of timezone
            {
                if (duration.HasValue)
                {
                    return "none, duration: " + duration.Value.ToString("hh\\:mm\\:ss");
                }

                return "(none)";
            }
            else
            {
                TimeZoneInfo tz = Clock.GetClock(Settings.Default.TzRegKeyName).TimeZoneInfo;
                DateTime dateAdjusted = date + tz.GetUtcOffset(date);
                return (Settings.Default.TzRegKeyName == "UTC")
                    ? date.ToString("u")
                    : dateAdjusted.ToString(CultureInfo.CurrentCulture);
            }
        }

        public static string GetDefinitionName(IBuildDefinition definition)
        {
            return (definition == null) ? "<none>" : definition.Name;
        }
    }
}
